Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
mongodb - match if a group of values from an array is in another given array [with Aggregation]

I'm looking for a solution since 3 hours and I don't get it yet.

I have the following collection:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 2', indexes:[1,3] },
{text: 'random text 3', indexes:[2,4] },

and I would like to have only the documents that have all the index values in a given array like for example [1,2,4]

using the example above, I would like to have the following output:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 3', indexes:[2,4] },

[1,2] is in [1,2,4] -> OK

[1,3] is not in [1,2,4] because of 3 -> Not OK

[1,4] is in [1,2,4] -> OK

Any idea? thanks for your answer! :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use one of this this $match stage:

  • Using $setIsSubset into $expr:
{
  "$match": {
    "$expr": {
      "$setIsSubset": ["$indexes",[1,2,4]]
    }
  }
}

Example here

  • Using $elemMatch and double negation ($not and $nin):
{
  "$match": {
    "indexes": {
      "$not": {
        "$elemMatch": {
          "$nin": [1,2,4]
        }
      }
    }
  }
}

Example here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!